import { Service } from 'egg';
import { Op } from 'sequelize';

export default class Message extends Service {

  async all(userId: number) {
    const { ctx } = this;
    return await ctx.model.Message.findAll({
      where: { sendUserId: userId },
    });
  }

  async history(userId: number, friendId: number) {
    const { ctx } = this;
    return await ctx.model.Message.findAll({
      where: {
        [Op.or]: [
          { sendUserId: userId, receiveUserId: friendId },
          { sendUserId: friendId, receiveUserId: userId },
        ],
      },
      order: [ 'time', 'DESC' ],
    });
  }
}
